package com.example.baseapp.common.exception;

import com.example.baseapp.common.enums.ResponseCode;
import lombok.Data;

/**
 * BusinessException class
 *
 * @author zhangl
 * @date 2022/4/30 17:30
 */
@Data
public class BusinessException extends RuntimeException {
    private Integer code;
    private String message;

    public BusinessException() {
    }

    public BusinessException(String message) {
        super(message);
    }

    public BusinessException(Integer code, String message) {
        this(message);
        this.code = code;
        this.message = message;
    }

    /**
     * 创建一个自定义异常
     *
     * @param code    异常码
     * @param message 异常信息
     * @return BusinessException
     */
    public static BusinessException createException(Integer code, String message) {
        return new BusinessException(code, message);
    }

    /**
     * 创建一个成功自定义异常
     *
     * @param message 异常信息
     * @return BusinessException
     */
    public static BusinessException createSuccessException(String message) {
        return new BusinessException(ResponseCode.SUCCESS.getCode(), message);
    }

    /**
     * 创建一个错误自定义异常
     *
     * @param message 异常信息
     * @return BusinessException
     */
    public static BusinessException createErrorException(String message) {
        return new BusinessException(ResponseCode.ERROR.getCode(), message);
    }
}
